package com.warm.demo.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.warm.demo.api.dto.TestMaterDto;
import com.warm.demo.api.vo.TestMaterVo;
import com.warm.demo.domain.TestMater;
import com.warm.demo.domain.TestSub;
import com.warm.demo.mapper.TestMaterMapper;
import com.warm.demo.service.ITestMaterService;
import com.warm.demo.service.ITestSubService;
import com.warm.mybatis.domain.QueryParam;
import com.warm.mybatis.utils.PageUtils;
import com.warm.core.page.TableDataInfo;
import com.warm.mybatis.service.impl.BaseServiceImpl;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.annotation.Tran;

import java.util.*;
import java.util.Map;

import static com.warm.demo.convert.TestMaterC.testMaterC;
import static com.warm.demo.domain.table.TestMaterTableDef.test_mater;
import static com.warm.demo.domain.table.TestSubTableDef.test_sub;
import static com.warm.mybatis.domain.QueryParam.getPage;

/**
 * 主子演示Service业务层处理
 *
 * @author warm
 * @date 2023-10-13
 */
@Component
public class TestMaterServiceImpl extends BaseServiceImpl<TestMaterMapper, TestMater> implements ITestMaterService {

    @Inject
    private ITestSubService testSubService;


    @Override
    public TestMaterVo selectById(Long id) {
        TestMater entity = getMapper().selectOneById(id);
        List<TestSub> subDoList = testSubService.list(test_sub.legal_id.eq(entity.getId()));
        entity.setTestSubList(subDoList);
        return testMaterC.convert(entity);
    }

    @Override
    public TableDataInfo<TestMaterVo> selectPage(QueryParam queryParam, TestMaterDto dto) {
        Page<TestMaterVo> page = buildQuery(dto).pageAs(getPage(queryParam), TestMaterVo.class);
        if (CollUtil.isEmpty(page.getRecords())) {
            page.setRecords(Collections.emptyList());
        }
        return PageUtils.build(page);
    }

    @Override
    public List<TestMaterVo> selectList(TestMaterDto dto) {
        return buildQuery(dto).listAs(TestMaterVo.class);
    }

    public QueryChain<TestMater> buildQuery(TestMaterDto dto) {
        Map<String, Object> params = dto.getParams();
        return this.queryChain()
                .where(test_mater.law_firm_name.like(dto.getLawFirmName()))
                .and(test_mater.address.eq(dto.getAddress()))
                .and(test_mater.file_id.eq(dto.getFileId()))
                .and(test_mater.state.eq(dto.getState()))
                .and(test_mater.publish_time.between(params.get("beginPublishTime"), params.get("endPublishTime"))
                        .when(!ObjectUtil.hasNull(params.get("beginPublishTime"), params.get("endPublishTime"))));
    }

    @Tran
    @Override
    public int insertByDto(TestMaterDto dto) {
        TestMater entity = testMaterC.convert(dto);
        int rows = getMapper().insertSelective(entity);
        insertTestSub(entity);
        return rows;
    }

    @Tran
    @Override
    public int updateByDto(TestMaterDto dto) {
        TestMater entity = testMaterC.convert(dto);
        testSubService.remove(test_sub.legal_id.eq(entity.getId()));
        insertTestSub(entity);
        return getMapper().update(entity);
    }

    @Tran
    @Override
    public int deleteByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        testSubService.remove(test_sub.legal_id.in(idList));
        return getMapper().deleteBatchByIds(idList);
    }

    private void insertTestSub(TestMater entity) {
        List<TestSub> subDoList = entity.getTestSubList();
        Long id = entity.getId();
        if (CollUtil.isNotEmpty(subDoList)) {
            subDoList.forEach(sub -> sub.setLegalId(id));
            testSubService.saveBatch(subDoList);
        }
    }
}
